import {
	URL_IP_HOST,
	URL_PREFIX
} from './constant'
// import { Message } from 'element-ui'; 	
// 默认异常处理
function _fail_call(err) {
	console.log('fail  ->>  ', err)
}

// 封装请求函数
export function my_request(url, {
	method,
	data,
	header,
	not_payload
}) {

	// 设置各参数默认值
	method = method === undefined ? 'GET' : method
	header = header === undefined ? {} : header
	// fail_call = fail_call === undefined ? _fail_call : fail_call
	// post请求数据, 响应数据默认JSON格式, 不需要JSON需要设置 not_json
	if (method === 'POST' && not_payload) {
		header['content-type'] = 'application/x-www-form-urlencoded'
	} else if (url == "/application/resume/parserResume") {

	} else {
		header['content-type'] = 'application/json'
	}
	var userInfoData_ = uni.getStorageSync("userInfoData_")
	header["Authorization"] = `Bearer ${userInfoData_ ? userInfoData_.access_token : ""}`;
	header["clientid"] = userInfoData_ ? userInfoData_.client_id : "eeb9377d4b6debbc0a6e8756bbbbb4c7"

	// console.log(nowtoken)
	// if (url != "/login/get_myinfo" || url != "/login/wx") {
	// 	console.log(url);
	// 	if (!nowtoken) {
	// 		uni.navigateTo({
	// 			url: "/pages/login/login"
	// 		})
	// 		return
	// 	}
	// }
	// header['H5Token'] = nowtoken
	let responseType = ""
	if (url == "/application/resume/exportResume") {
		responseType = "arraybuffer"

	}
	if(url == "/application/oss/upload"){
		responseType = "application/json"
	}
	let that = this
	return new Promise((resolve, reject) => {
		uni.request({
			url: URL_IP_HOST + URL_PREFIX + url,
			method: method,
			data: data,
			responseType: responseType,
			header: header,
			success: (res) => {
				if (url == "/application/resume/exportResume") {
					resolve(res)
					return
				}
				// console.log(res);
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					uni.$u.toast( res.data.msg)
					setTimeout(() => {
						uni.navigateTo({
							url: "/pages/login/login"
						})
					}, 2000)
					return
				} else {
					console.log(res);
					uni.$u.toast( res.data.msg)
					reject(res.data)
				}
			},
			fail: (res) => {
				console.log(res);
				uni.$u.toast('操作失败！请联系管理员！')
				reject(new Error(res.msg || 'Error'))
			},
			complete: (res) => {
				reject(new Error(res.msg || 'Error'))
			},
		})
	})


}
